﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MarkdownSharp;
using System.Web.Mvc;
using Subtle.Domain.Models;
using System.Text;
using Subtle.ViewModels;

namespace Subtle.Extensions
{
	/// <summary>
	/// Html Extension
	/// </summary>
	public static class HtmlExtensions
	{
		/// <summary>
		/// Tag For
		/// </summary>
		/// <typeparam name="TModel"></typeparam>
		/// <typeparam name="TValue"></typeparam>
		/// <param name="html"></param>
		/// <param name="expression"></param>
		/// <returns></returns>
		public static MvcHtmlString TagsForPost(this HtmlHelper html, Post post)
		{
			if (post == null)
			{
				return new MvcHtmlString("<input id=\"Tags\" name=\"Tags\"></input>");
			}

			string tags = String.Empty;
			tags = String.Join(",", post.Tags.Select((t) => t.Description));
			return new MvcHtmlString(String.Format("<input id=\"Tags\" name=\"Tags\" value=\"{0}\"></input>", tags));
		}

		/// <summary>
		/// Display Comments for Post
		/// </summary>
		/// <param name="html"></param>
		/// <param name="postViewModel"></param>
		/// <returns></returns>
		public static MvcHtmlString DisplayCommentsForPost(this HtmlHelper html, string shortName, HomePostViewModel postViewModel)
		{
			StringBuilder commentsBuilder = new StringBuilder();
			commentsBuilder.Append("<div id=\"disqus_thread\">");
			commentsBuilder.Append("</div>");
			commentsBuilder.Append("<a href=\"http://disqus.com\" class=\"dsq-brlink\">blog comments powered by <span class=\"logo-disqus\">");
			commentsBuilder.Append("Disqus</span></a>");
			commentsBuilder.Append("<script type=\"text/javascript\">");
			commentsBuilder.Append("var disqus_shortname = '" + shortName + "';");
			commentsBuilder.Append("var disqus_identifier = '" + postViewModel.ID + "';");
			commentsBuilder.Append("var disqus_url = '" + HttpContext.Current.Request.Url + "';");
			commentsBuilder.Append("var disqus_developer = 0;");

			/* * * DON'T EDIT BELOW THIS LINE * * */
			commentsBuilder.Append("$(document).ready(function () {");
			commentsBuilder.Append("var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;");
			commentsBuilder.Append("dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';");

			commentsBuilder.Append("(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);");
			commentsBuilder.Append("})();");
			commentsBuilder.Append("</script>");
			commentsBuilder.Append("<noscript>");
			commentsBuilder.Append("Please enable JavaScript to view the <a href=\"http://disqus.com/?ref_noscript\">comments");
			commentsBuilder.Append("powered by Disqus.</a>");
			commentsBuilder.Append("</noscript>");
			return MvcHtmlString.Create(commentsBuilder.ToString());
		}

		/// <summary>
		/// Display Share for Post
		/// </summary>
		/// <param name="html"></param>
		/// <param name="postViewModel"></param>
		/// <param name="username"></param>
		/// <returns></returns>
		public static MvcHtmlString DisplayShareForPost(this HtmlHelper html, HomePostViewModel postViewModel, string username)
		{
			StringBuilder shareBuilder = new StringBuilder();
			//Twitter
			shareBuilder.Append("<a href=\"http://twitter.com/share\" class=\"twitter-share-button\" data-url=\"" + HttpContext.Current.Request.Url + "\" data-text=\"" + postViewModel.Title + "\" data-count=\"vertical\" data-via=\"" + username + "\">");
			shareBuilder.Append("Tweet");
			shareBuilder.Append("</a>");
			shareBuilder.Append("<script type=\"text/javascript\" src=\"http://platform.twitter.com/widgets.js\"></script>");
			return MvcHtmlString.Create(shareBuilder.ToString());
		}
	}

	/// <summary>
	/// Helper class for transforming Markdown.
	/// </summary>
	public static class MarkdownHelper
	{
		/// <summary>
		/// An instance of the Markdown class that performs the transformations.
		/// </summary>
		private static Markdown markdownTransformer = new Markdown();

		/// <summary>
		/// Transforms a string of Markdown into HTML.
		/// </summary>
		/// HtmlHelper - Not used, but required to make this an extension method.
		/// The Markdown that should be transformed.
		/// The HTML representation of the supplied Markdown.
		public static IHtmlString Markdown(this HtmlHelper helper, string text)
		{
			// Transform the supplied text (Markdown) into HTML.
			string html = markdownTransformer.Transform(text);

			// Wrap the html in an MvcHtmlString otherwise it'll be HtmlEncoded and displayed to the user as HTML :(
			return new MvcHtmlString(html);
		}
	}
}